//
//  OrderViewController.m
//  chongwu
//
//  Created by 隼目 on 2021/9/15.
//

#import "OrderViewController.h"
#import "OrderTableViewCell.h"
#import "OrderDetailsViewController.h"
@interface OrderViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,orderDelegate>

@property(nonatomic,strong)UIView *bottomView;
@property(nonatomic,strong)BaseTableView *tableView;
@property(nonatomic,strong)UITextField *searchText;
@property(nonatomic,strong)NSArray<UIImageView *> *imageArr;
@property(nonatomic,strong)NSArray<UILabel *> *titleArr;
@property(nonatomic,strong)NSArray<UILabel *> *xianArr;
@property(nonatomic,strong)NSMutableArray<UIButton *> *btnArr;
@property(nonatomic,assign)NSInteger selectIndex;
@property(nonatomic,assign)NSInteger selectSendOrCollect;

@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self cretTopView];
}
-(void)cretTopView{
    self.searchText=[UITextField new];
    [self.view addSubview:self.searchText];
    [self.searchText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28*_mainWidth);
        make.right.mas_equalTo(-28*_mainWidth);
        make.top.mas_equalTo(95*_mainHeight);
        make.height.mas_equalTo(69*_mainHeight);
    }];
    [self.searchText setReturnKeyType:UIReturnKeySearch];
    self.searchText.backgroundColor=[UIColor whiteColor];
    self.searchText.layer.cornerRadius=69*_mainHeight/2;
    self.searchText.layer.masksToBounds=YES;
    
    UIView *leftView=[UIView new];
    [leftView setFrame:CGRectMake(0, 0, 82*_mainWidth, 32*_mainWidth)];
    UIImageView *leftSearcgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(25*_mainWidth, 0, 32*_mainWidth, 32*_mainWidth)];
    leftSearcgImgView.image = [UIImage imageNamed:@"sousuo"];
    leftSearcgImgView.contentMode = UIViewContentModeCenter;
    [leftView addSubview:leftSearcgImgView];
    self.searchText.delegate=self;
    self.searchText.leftView = leftView;
    self.searchText.leftViewMode = UITextFieldViewModeAlways;
    self.searchText.placeholder=@"请输入订单号/运号";
    self.searchText.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(26)];
    
    UIView *btnView=[UIView new];
    [self.view addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.searchText.mas_bottom);
        make.height.mas_equalTo(101*_mainHeight);
    }];
    
    UIView *xianView=[UIView new];
    [btnView addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.height.mas_equalTo(65*_mainHeight);
        make.width.mas_equalTo(2*_mainWidth);
    }];
    xianView.backgroundColor=[UIColor whiteColor];
    
    UIView *leftView2=[UIView new];
    [btnView addSubview:leftView2];
    [leftView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(xianView.mas_left);
    }];
    leftView2.tag=0;
    leftView2.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectSendOrCollect:)];
    [leftView2 addGestureRecognizer:tapGesture];
    
    UIImageView *leftImage=[UIImageView new];
    [leftView2 addSubview:leftImage];
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(110*_mainWidth);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(35*_mainWidth);
    }];
    leftImage.image=[UIImage imageNamed:@"liang"];
    
    UILabel *leftLabel=[UILabel new];
    [leftView2 addSubview:leftLabel];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(leftImage.mas_right).mas_equalTo(27*_mainWidth);
    }];
    [leftLabel layoutIfNeeded];
    leftLabel.text=@"我寄的";
    leftLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(36)];
    leftLabel.textColor=rgba(255,255,255,1);
    
    UIView *leftXian=[UIView new];
    [leftView2 addSubview:leftXian];
    [leftXian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(leftLabel);
        make.width.mas_equalTo(71*_mainWidth);
        make.height.mas_equalTo(8*_mainHeight);
        make.top.mas_equalTo(leftLabel.mas_bottom).mas_equalTo(6*_mainHeight);
    }];
    leftXian.layer.cornerRadius=8*_mainHeight;
    leftXian.backgroundColor=rgba(255,255,255,1);
    
    UIView *rightView=[UIView new];
    [btnView addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(xianView.mas_right);
    }];
    rightView.tag=1;
    rightView.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectSendOrCollect:)];
    [rightView addGestureRecognizer:tapGesture2];
    
    UIImageView *rightImage=[UIImageView new];
    [rightView addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-110*_mainWidth);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(35*_mainWidth);
    }];
    rightImage.image=[UIImage imageNamed:@"an-1"];
    
    UILabel *rightLabel=[UILabel new];
    [rightView addSubview:rightLabel];
    [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(rightImage.mas_left).mas_equalTo(-27*_mainWidth);
    }];
    [rightLabel layoutIfNeeded];
    rightLabel.text=@"我收的";
    rightLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(36)];
    rightLabel.textColor=rgba(255,255,255,0.6);
    
    UIView *rightXian=[UIView new];
    [rightView addSubview:rightXian];
    [rightXian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(rightLabel);
        make.width.mas_equalTo(71*_mainWidth);
        make.height.mas_equalTo(8*_mainHeight);
        make.top.mas_equalTo(rightLabel.mas_bottom).mas_equalTo(6*_mainHeight);
    }];
    rightXian.layer.masksToBounds=YES;
    rightXian.layer.cornerRadius=8*_mainHeight;
    rightXian.backgroundColor=rgba(255,255,255,1);
    rightXian.hidden=YES;
    
    self.imageArr=@[leftImage,rightImage];
    self.titleArr=@[leftLabel,rightLabel];
    self.xianArr=@[leftXian,rightXian];
    
    self.bottomView=[UIView new];
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(btnView.mas_bottom);
        make.height.mas_equalTo(105*_mainHeight);
    }];
    self.bottomView.backgroundColor=[UIColor whiteColor];
    [self creatBtnView];
    
    self.tableView=[BaseTableView new];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.bottomView.mas_bottom);
    }];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor=rgba(245, 246, 246, 1);
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}
-(void)creatBtnView{
    for(UIView *view in [self.bottomView subviews]){
        [view removeFromSuperview];
    }
    NSArray *btnArr;
    if (self.selectSendOrCollect==0) {
        btnArr=[NSArray arrayWithObjects:@"全部",@"待付款",@"待取件",@"待签收",@"其他 ", nil];
    }else if (self.selectSendOrCollect==1) {
        btnArr=[NSArray arrayWithObjects:@"全部",@"待签收",@"其他 ", nil];
    }
    
    CGFloat width=_mainW/btnArr.count;
    self.btnArr=[NSMutableArray arrayWithCapacity:5];
    for (int i=0; i<btnArr.count; i++) {
        UIView *backeView=[UIView new];
        [self.bottomView addSubview:backeView];
        [backeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(width*i);
            make.width.mas_equalTo(width);
            make.top.bottom.mas_equalTo(0);
        }];
        
        UIButton *btn=[UIButton new];
        [backeView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            make.width.mas_equalTo(123*_mainWidth);
            make.height.mas_equalTo(47*_mainHeight);
        }];
        btn.tag=i;
        [btn setTitle:btnArr[i] forState:0];
        [btn addTarget:self action:@selector(selectOrderStatus:) forControlEvents:UIControlEventTouchUpInside];
        if (self.selectIndex==i) {
            btn.layer.masksToBounds=YES;
            btn.layer.borderColor=rgba(250,100,0,1).CGColor;
            btn.layer.borderWidth=2*_mainHeight;
            btn.layer.cornerRadius=47*_mainHeight/2;
            [btn setTitleColor:rgba(250,100,0,1) forState:0];
            btn.titleLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(30)];
        }else{
            btn.layer.borderWidth=0;
            [btn setTitleColor:rgba(171,172,174,1) forState:0];
            btn.titleLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(28)];
        }
        [self.btnArr addObject:btn];
    }
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden=YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden=NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OrderTableViewCell *cell=[[OrderTableViewCell alloc] init];
    cell.delegate=self;
    cell.tag=indexPath.row;
    [cell creatView];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  357*_mainHeight;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderDetailsViewController *vc=[[OrderDetailsViewController alloc] init];
    vc.stateIndex=indexPath.row;
    vc.isHidenDog=YES;
    [ZQTools pushRootViewController:self andNextController:vc];
}
-(void)selectSendOrCollect:(UITapGestureRecognizer *)tap{
    self.selectSendOrCollect=tap.view.tag;
    self.selectIndex=0;
    for (int i=0; i<2; i++) {
        UILabel *label=self.titleArr[i];
        UIView *xian=self.xianArr[i];
        if (tap.view.tag==i) {
            xian.hidden=NO;
            label.textColor=rgba(255,255,255,1);
        }else{
            xian.hidden=YES;
            label.textColor=rgba(255,255,255,0.6);
        }
    }
    UIImageView *leftImage=self.imageArr[0];
    UIImageView *rightImage=self.imageArr[1];
    if (tap.view.tag==0) {
        leftImage.image=[UIImage imageNamed:@"liang"];
        rightImage.image=[UIImage imageNamed:@"an-1"];
    }else if (tap.view.tag==1) {
        leftImage.image=[UIImage imageNamed:@"an"];
        rightImage.image=[UIImage imageNamed:@"liang-1"];
    }
    [self creatBtnView];
}
-(void)selectOrderStatus:(UIButton *)button{
    self.selectIndex=button.tag;
    for (int i=0; i<self.btnArr.count; i++) {
        UIButton *btn=self.btnArr[i];
        if (self.selectIndex==i) {
            btn.layer.masksToBounds=YES;
            btn.layer.borderColor=rgba(250,100,0,1).CGColor;
            btn.layer.borderWidth=2*_mainHeight;
            btn.layer.cornerRadius=47*_mainHeight/2;
            [btn setTitleColor:rgba(250,100,0,1) forState:0];
            btn.titleLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(30)];
        }else{
            btn.layer.borderWidth=0;
            [btn setTitleColor:rgba(171,172,174,1) forState:0];
            btn.titleLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(28)];
        }
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
